<template>
<view class="serach-box">
    <uni-search-bar  @input="input" cancelButton='none' radius="50"></uni-search-bar>

  <!-- 搜索建议列表 -->
  <view class="sugg-list" v-if="searchResults.length !== 0">
    <view class="sugg-item" v-for="item in searchResults"  :key='item.goods_id' @click="gotoDetail(item)">
      <view class="goods_name">{{item.goods_name}}</view>
      <uni-icons type="arrow-right" size="16"></uni-icons>
    </view>
  </view>
  <!-- 搜索历史区域 -->
  <view class="history-box" v-else>
    <view class="history-title">
      <text>搜索历史</text>
      <uni-icons type="trash" size="25" @click="cleanHistory"></uni-icons>
    </view>
    <view class="history-list" >
      <uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList()"></uni-tag>
    </view> 

  </view>
</view>
</template>

<script>
  export default {
    data() {
      return {
        //延时器id
        timer:null,
        //输入框内容
        kw:'',
        searchResults:[],
        historyList:['a','b','c'],
        // isNull:true
      };
    },
    onload(){
      this.historyList=JSON.parse(uni.getStorageSync('kw')||[])
    },
    //计算属性
    computed:{
      //将搜索历史列表中的内容翻转，使最新的搜索历史在第一个
      histories(){
        return [...this.historyList].reverse()
      }
    },
    
    methods:{
      input(e){
        //进行防抖处理
        clearTimeout(this.timer);
        this.timer=setTimeout(()=>{
          this.kw=e;
          this.getSearchList();
        } ,500)
      },
      //获取搜索建议列表
      async getSearchList(){
        try{
          if(this.kw===''){
           this.searchResults=[]
           return
           }else{
          const {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
          if(res.meta.status!==200) return uni.$showMsg();
          this.searchResults=res.message;}
        }catch(Error){
          uni.$showMsg(Error)
        }
        this.SavesearchList();
      } ,
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      //保存搜索历史
      SavesearchList(){
        let set=new Set(this.historyList);
        set.delete(this.kw);
        set.add(this.kw);
        this.historyList=Array.from(set);
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
   
      //清除搜索历史
      cleanHistory(){
        this.historyList=[],
        uni.setStorageSync('kw','')
      },
      gotoGoodsList(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+kw
        })
        }
  }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list{
  padding: 0 5px;
  .sugg-item{
    //flex布局
    display: flex;
    //在交叉轴上居中对齐
    align-items: center;
    //space-between的值意味着项目之间的间隔是相等的，但是第一个项目会紧贴容器的开始边缘，最后一个项目会紧贴容器的结束边缘。这样，项目之间的空白空间就被均匀地分布了，但是不会在容器的两端留下空白。
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid black;
    font-size: 15px;
    .goods_name{
      // 文字不换行
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 溢出部分用三个点号代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
.history-box{
  padding: 0 5px;
  .history-title{
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #efefef;
    height: 40px;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    .uni-tag{
      margin: 5px 5px 0 0;
    }
  }
}
</style>
